﻿param($exportPath)

Import-Module ..\Windows\PSRemoteRegistry\PSRemoteRegistry.psm1
Import-Module ActiveDirectory



workflow Start-MUILanguageReportGeneration
{
    param($exportPath)
    $computers=(Get-ADComputer -Filter {(name -notlike "*v-*")}).Name | Sort-Object 
    
    foreach -Parallel($computer in $computers)
    {
        InlineScript 
        {
            Import-Module c:\codeplex\Windows\PSRemoteRegistry\PSRemoteRegistry.psm1

            $connectionTest=Test-Connection -Count 1 -ComputerName $Using:computer -ErrorAction SilentlyContinue
            if($connectionTest)
            {
                Write-Output "Checking Remote Registry on $Using:computer..."
                $serviceState=Get-Service -ComputerName $Using:computer -Name RemoteRegistry -ErrorAction SilentlyContinue
                if($serviceState.Status -eq "Stopped")
                {
                    Write-Output "Starting service on $Using:computer..."
                    Get-Service -ComputerName $Using:computer -Name RemoteRegistry -ErrorAction SilentlyContinue | Start-Service -ErrorAction SilentlyContinue
                    Write-Output "Done!"
                }
                elseif($serviceState.Status -eq "Running")
                {
                    Write-Output "Service is running on $Using:computer"
                }
                else
                {
                    Write-Output "Service is in"$serviceState.Status"on"$Using:computer
                }
                
                
                Write-Output "Retrieving User languages from $Using:computer"
                $userKeys=Get-RegKey -Hive Users -Key "\" -ComputerName $using:computer| Where-Object {$_.Key -notlike "*_Classes" -and $_.Key.Length -gt 10}

                foreach($userKey in $userKeys)
                {
                    $keyName=$userKey.Key.Replace("\\","") + "\Volatile Environment"
                    $userName=Get-RegString -ComputerName $using:computer -Hive Users -Key $keyName -Value "USERNAME"
                    $keyName=$userKey.Key.Replace("\\","") + "\Control Panel\Desktop\MuiCached"
                    $muiLanguages=Get-RegMultiString -ComputerName $using:computer -Hive Users -Key $keyName  -Value "MachinePreferredUILanguages"
                    foreach($muiLanguage in $muiLanguages)
                    {
                        New-Object PSObject -Property @{ComputerName=$using:computer;UserName=$userName.Data;Language=$muiLanguage.Data[0];}| Export-csv -Path $using:exportPath\UserLanguages\$using:computer.csv -NoTypeInformation -Append
                    }
                }   
                
            }
            else
            {
                Write-Output "Cannot reach $Using:computer"
            }
        }
    }
}

Start-MUILanguageReportGeneration -exportPath $exportPath

Get-ChildItem $exportPath\UserLanguages\*.csv | ForEach-Object {Import-Csv $_} | Export-Csv -NoTypeInformation $exportPath\UserLanguagesReport.csv


